import type { ShapeType } from '@/interface';
import { DEFAULTLAYER } from '@/constant';

interface PropsType {
  image: HTMLImageElement;
  startX: number;
  startY: number;
  width: number;
  height: number;
}

class Image implements ShapeType {
  private type: string;
  private layer: number;
  image: HTMLImageElement;
  startX: number;
  startY: number;
  width: number;
  height: number;
  constructor(props: PropsType) {
    this.type = 'image';
    this.layer = DEFAULTLAYER - 1;
    this.image = props.image;
    this.startX = props.startX;
    this.startY = props.startY;
    this.width = props.width;
    this.height = props.height;
  }

  get src() {
    return this.image.src;
  }

  getType(): string {
    return this.type;
  }

  getLayer(): number {
    return this.layer;
  }

  setLayer(layer: number) {
    this.layer = layer;
  }

  changePosition(diffX: number, diffY: number) {
    this.startX += diffX;
    this.startY += diffY;
  }

  inside(x: number, y: number): boolean {
    if (
      x >= this.startX &&
      x <= this.startX + this.width &&
      y >= this.startY &&
      y <= this.startY + this.height
    ) {
      return true;
    }
    return false;
  }

  draw(ctx: CanvasRenderingContext2D) {
    ctx.drawImage(this.image, this.startX, this.startY, this.width, this.height);
  }
}

export default Image;
